//
//  BookingViewController.m
//  ceshiNET
//
//  Created by mk on 15/8/18.
//  Copyright (c) 2015年 mk. All rights reserved.
//

#import "BookingViewController.h"
#import "BookingTableViewCell.h"
#import "UILabelF.h"
@interface BookingViewController ()<UITableViewDataSource,UITableViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)NSMutableArray *dateArray;
@property (nonatomic, strong)UIPickerView *picker;
@property (nonatomic, strong)NSString *date;
@property (nonatomic, strong)UILabelF *name;
@property (nonatomic, strong)UILabelF *book;
@property (nonatomic, strong)UILabelF *dateLabelF;

@end

@implementation BookingViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.picker.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    self.array = [NSMutableArray arrayWithObjects:@"患者姓名",@"预约",@"就诊时间", nil];
    self.dateArray = [NSMutableArray array];
    NSDate *date = [NSDate date];
    for (NSInteger i = 0; i < 4; i ++) {
        NSTimeInterval week = 60 *60 * 24 *6 * i;
        NSTimeInterval week1 = 60 *60 * 24 *6 * (i + 1);
        NSDate *one = [NSDate dateWithTimeInterval: week sinceDate:date];
        NSDate *two = [NSDate dateWithTimeInterval: week1 sinceDate:date];
        NSDateFormatter *format = [[NSDateFormatter alloc]init];
        
        [format setDateFormat:@"MM月dd日"];
        NSString *stringDate = [[[format stringFromDate:one]stringByAppendingString:@" 至 " ]stringByAppendingString:[format stringFromDate:two]];
        [self.dateArray addObject:stringDate];
    }

    
    
    [self createView];
}
- (void)handleData
{
    
}
- (void)createView{
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    self.name = [[UILabelF alloc]initWithFrame:CGRectMake(0, 100, width, 40)];
    self.name.Tlabel.text = @"患者姓名 :";
    [self.view addSubview:self.name];
    
    self.book = [[UILabelF alloc]initWithFrame:CGRectMake(0, 140, width, 40)];
    self.book.Tlabel.text = @"预约:";
    self.book.testF.text = self.booking;
    self.book.testF.enabled = NO;
    [self.view addSubview:self.book];
    
    self.dateLabelF = [[UILabelF alloc]initWithFrame:CGRectMake(0, 180, width, 40)];
    self.dateLabelF.Tlabel.text = @"预约日期:";
    self.dateLabelF.testF.text = self.date;
    self.dateLabelF.testF.enabled = NO;
    self.dateLabelF.testF.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self.dateLabelF.testF addGestureRecognizer:tap];
    
    [self.view addSubview:self.dateLabelF];
    
    
}
- (void)tapAction{
    self.picker.hidden = NO;
}
- (void)create
{
    
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[BookingTableViewCell class] forCellReuseIdentifier:@"booking"];
    
    self.picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 2 * height / 3, width ,height / 3 )];
    self.picker.delegate = self;
    self.picker.dataSource = self;
    [self.view addSubview:self.picker];
    self.picker.backgroundColor = [UIColor grayColor];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"booking" forIndexPath:indexPath];
    cell.titleLabel.text = [self.array objectAtIndex:indexPath.row];
    if (indexPath.row == 1) {
        cell.textF.enabled = NO;
        cell.textF.text = self.booking;
    }
    if (indexPath.row == 2) {
        cell.textF.enabled = NO;
        cell.textF.text = self.date;
        cell.textF.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pikerAppear)];
        [cell.textF addGestureRecognizer:tap];
    }
    
    return cell;
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dateArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.dateArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.picker.hidden = YES;
    self.date =  [ self.dateArray objectAtIndex:row];
}
- (void)pikerAppear{
    self.picker.hidden = NO;
    self.tableView.backgroundColor = [UIColor yellowColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
